package com.anjoyo.xgh.musicshare.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.application.SongApplication;
import com.anjoyo.xgh.musicshare.entry.Song;
import com.anjoyo.xgh.musicshare.util.StringUtil;

public class DownSongAdapter extends BaseAdapter {
	ArrayList<Song> songs;
	LayoutInflater inflater;
	HashMap<String, String> deletmap;
	boolean isshowcheckbox = false;
	int pos = -1;

	public DownSongAdapter(Context context, ArrayList<Song> songs) {
		this.songs = songs;
		inflater = LayoutInflater.from(context);
		deletmap = new HashMap<String, String>();
	}

	@Override
	public int getCount() {
		return songs.size();
	}

	@Override
	public Song getItem(int arg0) {
		return songs.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	public void changeColor(int pos) {
		this.pos = pos;
		notifyDataSetChanged();
	}

	public void deletsong(int pos) {
		songs.remove(pos);
		notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder h;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.downsong_item, null);
			h = new ViewHolder();
			h.pos = (TextView) convertView.findViewById(R.id.tv_num);
			h.namesinger = (TextView) convertView
					.findViewById(R.id.tv_songname);
			h.checkBox = (CheckBox) convertView.findViewById(R.id.cb_1);
			convertView.setTag(h);
		} else {
			h = (ViewHolder) convertView.getTag();
		}
		if (position == pos) {
			h.pos.setTextColor(Color.rgb(40, 194, 241));
			h.namesinger.setTextColor(Color.rgb(40, 194, 241));
		} else {
			h.pos.setTextColor(Color.BLACK);
			h.namesinger.setTextColor(Color.BLACK);
		}
		String str = deletmap.get("" + songs.get(position).getMusicid());
		if (isshowcheckbox) {
			h.checkBox.setVisibility(View.VISIBLE);
		} else {

			h.checkBox.setVisibility(View.GONE);
		}
		h.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					deletmap.put("" + songs.get(position).getMusicid(), "1");
				} else {
					deletmap.put("" + songs.get(position).getMusicid(), "0");
				}
//				System.out.println(position + "选择了" + isChecked);
			}
		});
		if (!StringUtil.isnull(str) && str.equals("1")) {
			h.checkBox.setChecked(true);
		} else {
			h.checkBox.setChecked(false);
		}
		h.pos.setText((position + 1) + "");
		h.namesinger.setText(songs.get(position).getName() + "-"
				+ songs.get(position).getArtist());
		return convertView;
	}

	class ViewHolder {
		TextView pos, namesinger;
		CheckBox checkBox;
	}

	public void deletsongs() {
		for (int i = 0; i < songs.size(); i++) {
			String str = deletmap.get("" + songs.get(i).getMusicid());
			if (!StringUtil.isnull(str) && str.equals("1")) {
				File file = new File(songs.get(i).getLocal_url());
				file.delete();
				SongApplication.getInstance().getDBhelper()
						.deletsong(songs.get(i).getMusicid());
				songs.remove(i);
			}
		}
		deletmap.clear();
		notifyDataSetChanged();
	}

	public void showcheckbox() {
		isshowcheckbox = true;
		notifyDataSetChanged();
	}
	public void unshowcheckbox() {
		isshowcheckbox = false;
		notifyDataSetChanged();
	}
}
